package com.hxf.service.preferential.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.hxf.common.PublicUtil;
import com.hxf.dto.MealMenuDto;
import com.hxf.dto.MealPreferentialDto;
import com.hxf.dto.RestaurantDto;
import com.hxf.dto.ReturnData;
import com.hxf.exception.DaoLevelException;
import com.hxf.exception.ServiceException;
import com.hxf.service.base.impl.BaseServiceImpl;
import com.hxf.service.preferential.PreferentialService;
import com.hxf.web.vo.PreferentialVo;

/**
 * service 餐厅优惠业务实现类
 * 
 * @author tengyicheng
 * @version [1.0, 2013/11/19]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Repository("preferentialService")
public class PreferentialServiceImpl extends BaseServiceImpl implements PreferentialService {

	@Override
	public ReturnData<MealPreferentialDto> findMealPreferential( MealPreferentialDto mealPreferentialDto, int targetPage, int pageSize) throws ServiceException {
		try {
			return super.daoFacade.getPreferentialDao().findMealPreferential(mealPreferentialDto, targetPage, pageSize);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		} 
	}
	
	@Override
	public ReturnData<PreferentialVo> findMealPreferentialByRestaurant(RestaurantDto restaurantDto, MealPreferentialDto mealPreferentialDto, int targetPage, int pageSize) throws ServiceException {
		try {
			return super.daoFacade.getPreferentialDao().findMealPreferentialByRestaurant(restaurantDto, mealPreferentialDto, targetPage, pageSize);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		} 
	}

	@Override
	public int updateReleaseStatus(String preferentialIds, boolean release) throws ServiceException {
		try { 
			if (PublicUtil.isNotNullAndEmpty(preferentialIds)) {
				String [] id  = preferentialIds.split(",");
				MealPreferentialDto rdto = null;
				for (String mpid : id) {
					rdto = (MealPreferentialDto)super.daoFacade.getPreferentialDao().get(MealPreferentialDto.class, Integer.parseInt(mpid)); 
					rdto.setReleaseStatus(release);
					super.update(rdto);
				}
			} else {
				return -1;
			}
			return 0;
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e); 
		}   
	}

	@Override
	public List<MealPreferentialDto> findMealPreferential(MealPreferentialDto mealPreferentialDto) throws ServiceException {
		try {
			return super.daoFacade.getPreferentialDao().findMealPreferential(mealPreferentialDto);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		} 
	}
	
	@Override
	public int updateStatus(String preferentialIds, Integer status) throws ServiceException {
		try { 
			if (PublicUtil.isNotNullAndEmpty(preferentialIds)) {
				String [] id  = preferentialIds.split(",");
				MealPreferentialDto rdto = null;
				for (String mpid : id) {
					rdto = (MealPreferentialDto)super.daoFacade.getPreferentialDao().get(MealPreferentialDto.class, Integer.parseInt(mpid)); 
					rdto.setStatus(status);
					super.update(rdto);
				}
			} else {
				return -1;
			}
			return 0;
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e); 
		}   
	}
}
